package com.school.edupoint.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.school.edupoint.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author lwlianghehe@gmail.com
 * @date 2025/05/29 11:57
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    TokenInterceptor tokenInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor)
                .excludePathPatterns("/api/login") // 登录接口不拦截
                .excludePathPatterns("/api/student/login") // 登录接口不拦截
                .excludePathPatterns("/api/activity/uploadphoto") // 上传图片接口不拦截
                .excludePathPatterns("/api/student/uploadphoto") // 上传学生图片接口不拦截
                .excludePathPatterns("/api/activity/save") // 保存接口不拦截
                .excludePathPatterns("/api/student/register") // 保存接口不拦截
                .excludePathPatterns("/api/test/**") // 测试接口不拦截
                .excludePathPatterns("/test-websocket.html") // 测试页面不拦截
                .excludePathPatterns("/ws") // WebSocket连接不拦截
                .excludePathPatterns("/ws/**") // WebSocket相关路径不拦截
                .excludePathPatterns("/*.html") // 静态HTML文件不拦截
                .excludePathPatterns("/static/**") // 静态资源不拦截
                .excludePathPatterns("/css/**", "/js/**", "/images/**") // 静态资源不拦截
                .addPathPatterns("/api/**"); // 只拦截API接口
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}
